
function Track(){
  this.id = null;
  this.name = null;
  this.status = null;
  this.distance=null;
  this.longitude=null;
  this.latitude=null;
}


function createTrack(){
  var xmlhttp;
  var txt,i;
  if(!isGPSEnabled()){
     return;
  }

  var url = "trackers.php?lon="+gpsLongitude+"&lat="+gpsLatitude+"&action=create";
  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  var trackid;
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState===4 && xmlhttp.status===200){
      var parser = new DOMParser();
      var x = parser.parseFromString(xmlhttp.responseText.trim(), "text/xml").getElementsByTagName("track");
      for (i=0;i<x.length;i++){
        trackid=x[i].getAttribute("id");
      }
    }
  };
  xmlhttp.open("POST",url,false);
  xmlhttp.setRequestHeader("Content-Type","text/xml");
  xmlhttp.overrideMimeType('text/xml');
  xmlhttp.send();
  return trackid;
}

function getTrackURL(trackid){
  return location.protocol + '//' + location.host + location.pathname + "?trackid="+trackid;  
}

function updateTrack(name,status,trackid){
  var xmlhttp;
  var txt,i;
  if(!isGPSEnabled()){
     return;
  }
  name = name.replace(/(<([^>]+)>)/ig, "");
  status = status.replace(/(<([^>]+)>)/ig, "");
  var url = "trackers.php?lon="+gpsLongitude+"&lat="+gpsLatitude+"&name="+encodeURIComponent(name)+"&status="+encodeURIComponent(status)+"&trackid="+trackid+"&action=update";
  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  var track = null;
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState===4 && xmlhttp.status===200){
      var parser = new DOMParser();
      var x = parser.parseFromString(xmlhttp.responseText.trim(), "text/xml").getElementsByTagName("track");
      track = new Track();
      for (i=0;i<x.length;i++){
        track.id=x[i].getAttribute("id");
        track.name=x[i].getAttribute("name");
        track.status=x[i].getAttribute("status");
        track.distance=x[i].getAttribute("distance");
        track.longitude=x[i].getAttribute("longitude");
        track.latitude=x[i].getAttribute("latitude");
      }
    }
  };
  xmlhttp.open("POST",url,false);
  xmlhttp.setRequestHeader("Content-Type","text/xml");
  xmlhttp.overrideMimeType('text/xml');
  xmlhttp.send();
  console.log("Track updated");
  return track;
}

function getPreyLocation(trackid){
  var xmlhttp;
  var txt,i;
  if(!isGPSEnabled()){
     return;
  }
  name = name.replace(/(<([^>]+)>)/ig, "");
  status = status.replace(/(<([^>]+)>)/ig, "");
  var url = "trackers.php?lon="+gpsLongitude+"&lat="+gpsLatitude+"&trackid="+trackid+"&action=getpreylocation";
  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  var track = null;
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState===4 && xmlhttp.status===200){
      var parser = new DOMParser();
      var x = parser.parseFromString(xmlhttp.responseText.trim(), "text/xml").getElementsByTagName("track");
      track = new Track();
      for (i=0;i<x.length;i++){
        track.id=x[i].getAttribute("id");
        track.name=x[i].getAttribute("name");
        track.status=x[i].getAttribute("status");
        track.distance=x[i].getAttribute("distance");
        track.longitude=x[i].getAttribute("longitude");
        track.latitude=x[i].getAttribute("latitude");
      }
    }
  };
  xmlhttp.open("POST",url,false);
  xmlhttp.setRequestHeader("Content-Type","text/xml");
  xmlhttp.overrideMimeType('text/xml');
  xmlhttp.send();
  return track;
}
function deleteTrack(trackid){
  var xmlhttp;
  var txt,i;
  if(!isGPSEnabled()){
     return;
  }
  name = name.replace(/(<([^>]+)>)/ig, "");
  status = status.replace(/(<([^>]+)>)/ig, "");
  var url = "trackers.php?lon="+gpsLongitude+"&lat="+gpsLatitude+"&trackid="+trackid+"&action=delete";
  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  var track = null;
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState===4 && xmlhttp.status===200){
      var parser = new DOMParser();
      var x = parser.parseFromString(xmlhttp.responseText.trim(), "text/xml").getElementsByTagName("track");
      track = new Track();
      for (i=0;i<x.length;i++){
        track.id=x[i].getAttribute("id");
        track.name=x[i].getAttribute("name");
        track.status=x[i].getAttribute("status");
        track.distance=x[i].getAttribute("distance");
        track.longitude=x[i].getAttribute("longitude");
        track.latitude=x[i].getAttribute("latitude");
      }
    }
  };
  xmlhttp.open("POST",url,false);
  xmlhttp.setRequestHeader("Content-Type","text/xml");
  xmlhttp.overrideMimeType('text/xml');
  xmlhttp.send();
  return track;
}

function locatePrey(){

}